package cn.jsj.cxsys.service.impl;

import org.springframework.stereotype.*;
import org.springframework.beans.factory.annotation.*;
import cn.jsj.cxsys.service.*;
import javax.annotation.*;
import cn.jsj.cxsys.dao.*;
import com.github.pagehelper.*;
import java.util.*;
import org.springframework.transaction.annotation.*;
import cn.jsj.cxsys.pojo.*;

@Service
public class TeacherLabServiceImpl implements TeacherLabService
{
    @Autowired
    private UserService userService;
    @Autowired
    private StudentLabService studentLabService;
    @Resource
    private TeacherDoLab teacherDoLab;
    @Resource
    private LabFunction labFunction;
    @Resource
    private UserFunction userFunction;

    @Override
    public List<LabMessage> getNotSeeLabMessage(final User teacher, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(teacher);
        if (inf.equals("teacher")) {
            PageHelper.startPage(pageNum, pageSize);
            return this.teacherDoLab.getApplyOfStudent(teacher.getUserID());
        }
        return null;
    }

    @Override
    public List<Lab> getAllLabAboutTeacher(final User teacher) {
        final String inf = this.userService.login(teacher.getAccount(), teacher.getPassword());
        if (inf.equals("teacher")) {
            final List<Lab> storage = this.studentLabService.getLabList();
            final LinkedList<Lab> result = new LinkedList<Lab>();
            for (final Lab i : storage) {
                if (i.getLaderTeleNumber().equals(teacher.getAccount().trim())) {
                    result.add(i);
                }
            }
            return result;
        }
        return null;
    }

    @Override
    public List<LabMessage> getAllLabMessage(final Teacher teacher, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(teacher);
        if (inf.equals("teacher")) {
            PageHelper.startPage(pageNum, pageSize);
            return this.teacherDoLab.getAllMessageOfTeacher(teacher.getUserID());
        }
        return null;
    }

    @Transactional
    @Override
    public String judgeStudentApply(final Teacher teacher, final LabMessage labMessage) {
        final User tea = this.userService.getUserByAccount(teacher.getAccount().trim(), teacher.getPassword().trim());
        if (tea == null) {
            return "error";
        }
        final String inf = this.userService.login(tea.getAccount(), tea.getPassword());
        if (!inf.equals("teacher")) {
            return "error";
        }
        if (tea.getUserID() == 0 || labMessage.getTeacherID() == 0 || tea.getUserID() != labMessage.getTeacherID()) {
            return "error";
        }
        int i = this.teacherDoLab.checkThisMessageExist(labMessage.getMessageID());
        if (i != 1) {
            return "thisMessageHadBeenDeleted";
        }
        final LabMessage labm = this.teacherDoLab.getLabMessageByID(labMessage.getMessageID());
        if (!labMessage.equals(labm)) {
            return "error";
        }
        final int j = this.labFunction.judgeTeacherIsTheLaderOfLab(tea.getUserID(), labm.getLabID());
        if (j == 0) {
            return "identityError";
        }
        if (labMessage.getResponseInf() == null || labMessage.getResponseInf().trim().length() < 1) {
            labMessage.setResponseInf("\u8001\u5e08\u4e0d\u60f3\u5bf9\u4f60\u8bf4\u4efb\u4f55\u4e1c\u897f\uff01");
        }
        labMessage.setReplyTime(NowDateFactory.getNowTime());
        if (labMessage.getResult() == 0) {
            i = this.teacherDoLab.updateTheMessage(labMessage);
            if (i == 1) {
                return "success";
            }
            return "error";
        }
        else {
            labMessage.setResult(1);
            i = this.teacherDoLab.updateTheMessage(labMessage);
            if (i != 1) {
                return "error";
            }
            i = this.teacherDoLab.allowStudent(labMessage.getLabID(), labMessage.getStudentID());
            if (i == 1) {
                return "success";
            }
            return "error";
        }
    }

    @Transactional
    @Override
    public String creatOneLab(final Lab lab, final Teacher teacher) {
        if (teacher == null) {
            return "paramLess";
        }
        final User iuser = this.userService.getUserByAccount(teacher.getAccount(), teacher.getPassword());
        final String inf = this.userService.login(iuser);
        if (!inf.equals("teacher")) {
            return inf;
        }
        if (lab == null || lab.getLabName() == null || lab.getLabName().trim().length() > 99 || lab.getLabName().trim().equals("") || lab.getLabNum() == null || lab.getLabNum().equals("") || lab.getLabNum().trim().equals("")) {
            return "paramLess";
        }
        if (!LetterNumberJudgeMent.isLetterOfNumber(lab.getLabNum())) {
            return "checkLabNum";
        }
        lab.setLabNum(lab.getLabNum().trim().toUpperCase());
        final Lab lab2 = this.labFunction.getLabByLabNum(lab.getLabNum());
        if (lab2 != null) {
            return "labExist";
        }
        final User user = this.userService.getUserByAccount(teacher.getAccount(), teacher.getPassword());
        lab.setLaderTeleNumber(user.getTeleNumber());
        lab.setLabNum(lab.getLabNum().toUpperCase());
        final int i = this.labFunction.insertOneLab(lab);
        if (i != 1) {
            return "error";
        }
        final Lab tt = this.labFunction.getLabByLabNum(lab.getLabNum());
        final int ii = this.teacherDoLab.setPrivelateWhenTeacherCreatLab(user.getUserID(), tt.getLabID());
        if (ii == 1) {
            return "success";
        }
        return "error";
    }

    @Override
    public User getStudentInformationOfApplyLab(final User teacher, final int userID) {
        if (userID < 1) {
            return null;
        }
        final String inf = this.userService.login(teacher);
        if (!"teacher".equals(inf)) {
            return null;
        }
        final User u = this.userFunction.getOneStudentById(userID);
        if (u == null) {
            return null;
        }
        u.setPassword("");
        return u;
    }

    @Override
    public List<User> getStudentsInLabAtThisTime(final User teacher, final int labID) {
        if (labID < 1) {
            return null;
        }
        final String inf = this.userService.login(teacher);
        if ("teacher".equals(inf)) {
            final int i = this.labFunction.judgeTeacherIsTheLaderOfLab(teacher.getUserID(), labID);
            if (i != 1) {
                return null;
            }
        }
        if ("error".equals(inf) || "student".equals(inf)) {
            return null;
        }
        return this.labFunction.getUserListOfLabThisTime(labID);
    }

    @Override
    public List<LabMessage> superLaderGetLabMessage(final String superaccount, final String password, final int pageNum, final int pageSize) {
        final String inf = this.userService.login(superaccount, password);
        if ("superLader".equals(inf)) {
            PageHelper.startPage(pageNum, pageSize);
            return this.teacherDoLab.getAllMessage();
        }
        if ("teacher".equals(inf)) {
            final User teacher = this.userService.getUserByAccount(superaccount.trim(), password.trim());
            return this.teacherDoLab.getAllMessageOfTeacher(teacher.getUserID());
        }
        return null;
    }

    @Transactional
    @Override
    public String changeDescriptionOfOneLab(final User user, final int labID, final String str) {
        final String inf = this.userService.login(user);
        if (labID < 1 || str == null || str.trim().length() == 0) {
            return "paramLess";
        }
        int i = this.labFunction.judgeTeacherIsTheLaderOfLab(user.getUserID(), labID);
        if (i != 1) {
            return "identityError";
        }
        i = this.labFunction.changeDescriptionOfLab(labID, str.trim());
        if (i == 0) {
            return "error";
        }
        return "success";
    }

    @Transactional
    @Override
    public String changeNameOfOneLab(final String account, final String password, final Integer labID, final String labName) {
        if (account == null || password == null || labName == null || labName.trim().length() == 0 || labName.trim().length() > 99) {
            return "paramLess";
        }
        final String inf = this.userService.login(account.trim(), password.trim());
        if (!inf.equals("teacher")) {
            return "identityError";
        }
        final User user = this.userService.getUserByAccount(account.trim(), password.trim());
        int i = this.labFunction.judgeTeacherIsTheLaderOfLab(user.getUserID(), labID);
        if (i != 1) {
            return "identityError";
        }
        i = this.labFunction.resetLabName(labID, labName.trim());
        if (i == 0) {
            return "error";
        }
        return "success";
    }

    @Override
    public int getMessageNumber() {
        return this.teacherDoLab.countMessages();
    }
}
